import { useState, useCallback } from 'react';

import { yqyj } from '@/api';

export default function useYqyj() {
  const [yjList, setYjList] = useState<any>({});
  const getYjList = useCallback(async (params: { YJQF: string }) => {
    const result = await yqyj.getYjList(params);
    console.log(result);
    setYjList(result);
  }, []);

  const [rankList, setRankList] = useState<any[]>([]);
  const getRankList = useCallback(async (params: { type: string }) => {
    const result = await yqyj.getRankList(params);
    console.log(result);
    if (result && result.data) {
      setRankList(result.data);
    }
  }, []);

  const [marker, setMarker] = useState<any>(undefined);
  const getMarker = useCallback(async (params: { ids: string }) => {
    const result: any = await yqyj.getMarker(params);
    console.log(result);
    if (result && result.length > 0) {
      const data = result[0];
      setMarker({
        lng: data.longitude || null,
        lat: data.latitude || null,
        title: data.name || '',
        id: data.id,
        type: data.type || null,
      });
    } else {
      setMarker(undefined);
    }
  }, []);
  const removeMarker = useCallback(() => {
    setMarker(undefined);
  }, []);

  const [houseList, setHouseList] = useState<any[]>([]);
  const getHouseList = useCallback(async (params: { type: string }) => {
    const result = await yqyj.getHouseList(params);
    if (result && result.data) {
      setHouseList(result.data);
    }
  }, []);

  return {
    yjList,
    getYjList,
    rankList,
    getRankList,
    marker,
    getMarker,
    removeMarker,
    houseList,
    getHouseList,
  };
}
